package org.jeecg.modules.dock.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.dock.client.EMSRequestClient;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.entity.DockDataSendRecord;
import org.jeecg.modules.dock.entity.dto.request.BaseRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.HZJReturnBucketRequest;
import org.jeecg.modules.dock.entity.dto.request.ZJCallBucketRequest;
import org.jeecg.modules.dock.entity.ems.response.EmsBaseResponseVO;
import org.jeecg.modules.dock.enums.DockDataStatus;
import org.jeecg.modules.dock.service.EmsDockingRepeaterService;
import org.jeecg.modules.dock.service.IDockDataSendRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ems对接数据服务
 *
 * @author fyl
 */
@Service
@Slf4j
@AllArgsConstructor
public class EmsDockingRepeaterServiceImpl implements EmsDockingRepeaterService {

    private final EMSRequestClient emsRequestClient;

    private final IDockDataSendRecordService dockDataSendRecordService;

    @Override
    public EmsBaseResponseVO zjCallBucket(ZJCallBucketRequest request) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(request));
        EmsBaseResponseVO emsBaseResponseVO = EmsBaseResponseVO.error();
        try {
            log.info("请求ems->叫纸筒数据：{}", request);
            emsBaseResponseVO = emsRequestClient.zjCallBucket(request);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(DockDataStatus.SUCCESS.getCode());
            record.setResultData(emsBaseResponseVO!=null?JsonUtils.toJson(emsBaseResponseVO):null);
            log.info("ems纵剪叫纸筒接口发送成功");
        } catch (Exception e) {
            log.info("ems纵剪叫纸筒接口出现异常, 数据id: {}, 时间: {}", record.getUuid(), DateUtil.date(), e);
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultCode(DockDataStatus.FAILURE.getCode());
        }
        dockDataSendRecordService.save(record);
        return emsBaseResponseVO;
    }

    @Override
    public EmsBaseResponseVO hzjReturnBucket(HZJReturnBucketRequest request) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(request));
        EmsBaseResponseVO emsBaseResponseVO = EmsBaseResponseVO.error();
        try {
            emsBaseResponseVO = emsRequestClient.hzjReturnBucket(request);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(DockDataStatus.SUCCESS.getCode());
            record.setResultData(emsBaseResponseVO!=null?JsonUtils.toJson(emsBaseResponseVO):null);
            log.info("ems横剪-纵剪退纸筒接口发送成功");
        } catch (Exception e) {
            log.info("ems横剪-纵剪退纸筒接口出现异常, 数据id: {}, 时间: {}", record.getUuid(), DateUtil.date(), e);
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultCode(DockDataStatus.FAILURE.getCode());
        }
        dockDataSendRecordService.save(record);
        return emsBaseResponseVO;
    }

    @Override
    public EmsBaseResponseVO forceFinish(BaseRequestDTO request) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(request));
        EmsBaseResponseVO emsBaseResponseVO = EmsBaseResponseVO.error();
        try {
            emsBaseResponseVO = emsRequestClient.forceFinish(request);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(DockDataStatus.SUCCESS.getCode());
            record.setResultData(emsBaseResponseVO != null ? JsonUtils.toJson(emsBaseResponseVO):null);
            log.info("ems强制完成接口发送成功");
        } catch (Exception e) {
            log.info("ems强制完成接口出现异常, 数据id: {}, 时间: {}", record.getUuid(), DateUtil.date(), e);
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultCode(DockDataStatus.FAILURE.getCode());
        }
        dockDataSendRecordService.save(record);
        return emsBaseResponseVO;
    }


    private DockDataSendRecord getRecord(String data) {
        DockDataSendRecord sendRecord = new DockDataSendRecord();
        sendRecord.setSource(SystemConstants.WMS);
        sendRecord.setTarget(SystemConstants.EMS);
//        sendRecord.setBusinessCode(SapInterfaceTypeEnum.SALE_OUTBOUND_POST.getCode());
        sendRecord.setUuid(IdUtil.fastSimpleUUID());
        sendRecord.setSendTime(new Date());
        sendRecord.setData(data);
        return sendRecord;
    }
}
